#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <math.h>

#define NOISY_TEST 1

//most of the structure and random functions were borrowed from betterTestDrawCard


int checkisGameOver(struct gameState *post, int gameOver) {
  int r;
  int errornum = 0;
  r = isGameOver(post); //call the real function and pass the state
  //assert(r == gameOver); //does the real function and the game over integer match?
  if (r != gameOver) {
	  errornum++;
  }

  //printf("Error %d \n", errornum);
  return 0;

}

int main() {
	int i;
	int k;
	int n;
	int gameOver;
	int randomSupplyNum;
	int emptyPiles;

	struct gameState G;

	printf ("Testing isGameOver():\n");

	//exact copy from betterTestDrawCard.c
	SelectStream(2);
	PutSeed(3);

	for (n = 0; n < 2000; n++) {
	    for (i = 0; i < sizeof(struct gameState); i++) {
	      ((char*)&G)[i] = floor(Random() * 256);
	    }

	// end of exact copy

	emptyPiles = 0; //number of empty piles
	//for every supply card
	for (k = 0; k < 25; k++)
	{
	  	 randomSupplyNum = floor(Random() * 12); //set a temporary variable to a random number
	     G.supplyCount[k] = randomSupplyNum; //set the supply card k to the random number
	     if (randomSupplyNum == 0) { //if the pile is out of cards
	       	 emptyPiles++; //increase to show that I have an empty pile now
	     }
	}

	if (G.supplyCount[3] == 0) { //if stack of Province cards is empty, the game ends
	   	gameOver = 1;
	}
	else if (emptyPiles >= 3) { //if three supply pile are at 0, the game ends
	   	gameOver = 1;
	}
	else {
	   	gameOver = 0;
	}


	checkisGameOver(&G, gameOver);
	}


	 printf ("ALL TESTS OK\n");

	return 0;
}
